package com.wutma.system.controller.sysuser;

import com.wutma.common.core.constants.HttpConstants;
import com.wutma.common.core.controller.BaseController;
import com.wutma.common.core.domain.R;
import com.wutma.common.core.domain.vo.LoginUserVO;
import com.wutma.system.domain.sysuser.dto.LoginDTO;
import com.wutma.system.domain.sysuser.dto.SysUserSaveDTO;
import com.wutma.system.service.sysuser.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/sysUser")
@RestController
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDTO loginDTO) {
        return sysUserService.login(loginDTO);
    }

    /**
     * 添加新用户(管理员)
     */
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO) {
        return toR(sysUserService.add(sysUserSaveDTO));
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return sysUserService.info(token);
    }

    /**
     * 退出登录
     */
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(sysUserService.logout(token));
    }
}
